package com.dmj.sqldsl.builder;

import com.dmj.sqldsl.builder.column.ColumnBuilder;
import com.dmj.sqldsl.builder.config.EntityConfig;
import com.dmj.sqldsl.builder.config.GlobalConfig;
import com.dmj.sqldsl.model.DslInsert;
import java.util.List;
import java.util.stream.Collectors;

public class InsertReturningBuilder<T, R> {

  private final InsertValuesBuilder<T> insertValuesBuilder;

  private final List<ColumnBuilder<T, R>> returningColumnBuilders;

  public InsertReturningBuilder(InsertValuesBuilder<T> tInsertValuesBuilder,
      List<ColumnBuilder<T, R>> columnBuilders) {
    this.insertValuesBuilder = tInsertValuesBuilder;
    this.returningColumnBuilders = columnBuilders;
  }

  private DslInsert toInsert(EntityConfig config) {
    DslInsert insert = insertValuesBuilder.toInsert(config);
    insert.setReturningColumns(returningColumnBuilders.stream().map(x -> x.build(config)).collect(
        Collectors.toList()));
    return insert;
  }

  public DslInsert toInsert() {
    return this.toInsert(GlobalConfig.getEntityConfig());
  }
}
